package com.shawn.integeration.batch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * @author shawn
 * @description: 一个Flow包含多个Step
 * @date 2024/1/7
 */
@Slf4j
//@Configuration
public class FlowJobBathConfig {
    @Resource
    private JobBuilderFactory jobFactory;
    @Resource
    private StepBuilderFactory stepFactory;

    @Bean
    public Job flowJob() {
        return jobFactory.get("flowJob")
                .start(flow())
                .next(flowStep3())
                .end()
                .build();
    }

    private Step flowStep1() {
        return stepFactory.get("flowStep1")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("flowStep1 执行步骤一。。。");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    private Step flowStep2() {
        return stepFactory.get("flowStep2")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("flowStep2 执行步骤二。。。");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    private Step flowStep3() {
        return stepFactory.get("flowStep3")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("flowStep3 执行步骤三。。。");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    /**
     * 创建一个Flow对象，包含若干个Step
     *
     * @return
     */
    private Flow flow() {
        return new FlowBuilder<Flow>("flow")
                .start(flowStep1())
                .next(flowStep2())
                .build();
    }
}
